package com.example.work4.common;

import static com.xuexiang.xui.utils.ResUtils.getResources;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

import com.example.work4.R;

public class BitmapUtil {
    public static Bitmap drawableToBitmap(Drawable drawable) {
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();

        if (w > 0 && h > 0) {
            try {
                Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
                Bitmap bitmap = Bitmap.createBitmap(w, h, config);
                Canvas canvas = new Canvas(bitmap);
                drawable.setBounds(0, 0, w, h);
                drawable.draw(canvas);
                return bitmap;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    public static Resources res = getResources();
    public static Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.vector_download_failed);

    public static Bitmap ava = BitmapFactory.decodeResource(res, R.mipmap.icon);
}
